<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require "smarty/Smarty.class.php";

/**
* @file system/application/libraries/Myview.php
*/
class Template extends Smarty
{
	function Template()
	{
		$this->Smarty();

		$config =& get_config();

		// absolute path prevents "template not found" errors
		$this->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir']
																	  : BASEPATH . 'application/views/');

		$this->compile_dir  = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir']
																	 : BASEPATH . 'cache/'); //use CI's cache folder
        //set smarty left_delimiter
        if(!empty($config['smarty_left_delimiter'])) {
            $this->left_delimiter  = $config['smarty_left_delimiter'];
        }
        //set smarty right_delimiter
        if(!empty($config['smarty_right_delimiter'])) {
            $this->right_delimiter  = $config['smarty_right_delimiter'];
        }
        //set site charset , utf-8 default
        if(!empty($config['charset'])) {
            $this->assign('charset',$config['charset']);
        }
        //set website url
		if (!empty($config['site_url'])) {
			$this->assign("site_url", $config['site_url']); // so we can get the full path to CI easily
		}
        //set js_url,css_url,image_url
        $this->assign("js_url", $config['js_url']);
        $this->assign("css_url", $config['css_url']);
        $this->assign("image_url", $config['image_url']);
        $this->assign("flash_url", $config['flash_url']);
	}

	/**
	* @param $resource_name string
	* @param $params array holds params that will be passed to the template
	* @desc loads the template
	*/
	function view($resource_name, $params = array())   {
		if (strpos($resource_name, '.') === false) {
			$resource_name .= '.tpl';
		}

		if (is_array($params) && count($params)) {
			foreach ($params as $key => $value) {
				$this->assign($key, $value);
			}
		}

		// check if the template file exists.
		if (!is_file($this->template_dir . $resource_name)) {
			show_error("template: [$resource_name] cannot be found.");
		}

		return parent::display($resource_name);
	}

	/**
	 * get cache content
	 *
	 * @param unknown_type $resource_name
	 * @param unknown_type $params
	 * @return unknown
	 */
	function get_html($resource_name, $params = array())   {
		if (strpos($resource_name, '.') === false) {
			$resource_name .= '.tpl';
		}

		if (is_array($params) && count($params)) {
			foreach ($params as $key => $value) {
				$this->assign($key, $value);
			}
		}

		// check if the template file exists.
		if (!is_file($this->template_dir . $resource_name)) {
			show_error("template: [$resource_name] cannot be found.");
		}

		return parent::fetch($resource_name);
	}
	
	/**
	 * 对数组循环赋值
	 *
	 * @param array $array
	 */
	function assign_array($array=array()){
		if (is_array($array) && count($array)) {
			foreach ($array as $key => $value) {
				$this->assign($key, $value);
			}
		}
		return ;
	}
} // END class smarty_library
?>